package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.xrit.sunnyday.dao.PermissionDao;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Daoying
 * creationTime: 2022/10/25 15:42
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /*
     * 查询所有权限
     * */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    /*
     * 分页
     * */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        // 拼接%
        if (!StringUtil.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }

        // 使用静态方法，pageheleper
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        // 紧接着的查询语句会被分页
        // Page 是pageHelper对象, 分页信息
        List<Permission> list = permissionDao.findPage(queryPageBean.getQueryString());

        PageInfo page = new PageInfo(list);
        // 包装到PageResult 再返回
        // 1 解耦
        // 2 total是基本类型，丢失。
        // 3 page对象内容过多 total,rows

        return new PageResult(page.getTotal(), page.getList());
    }

    /*
     * 添加
     * */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /*
     * 通过id查询
     * */
    @Override
    public Permission findById(int id) {
        return permissionDao.findById(id);
    }

    /*
     * 修改
     * */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /*
     * 删除
     * */
    @Override
    public void delete(int id) {
        permissionDao.delete(id);
    }
}
